package com.jxust.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxust.mapper.ClazzMapper;
import com.jxust.pojo.Clazz;
import com.jxust.pojo.ClazzQueryParam;
import com.jxust.pojo.Emp;
import com.jxust.pojo.PageResult;
import com.jxust.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        //1.获取分页参数size和page
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        //2.获取数据
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);
        //返回给controller
        Page<Clazz> p=(Page<Clazz>)clazzList;
        return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }

    @Override
    public boolean deleteById(Integer id) {
        int count = clazzMapper.countStudentsByClazzId(id);
        if (count > 0) {
            throw new RuntimeException("该班级下还有学生，无法删除");
        }
        return clazzMapper.deleteById(id) > 0;
    }

    @Override
    public void add(Clazz clazz) {
        LocalDateTime now = LocalDateTime.now();
        clazz.setCreateTime(now);
        clazz.setUpdateTime(now);
        clazzMapper.insert(clazz);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now()); // 设置更新时间
        clazzMapper.update(clazz);
    }
}

